Ext.define('qjtravel.controller.ImgUpController', {
    extend: 'Ext.app.Controller',
    config: {
        imgURL: "",
        refs: {
            camBtn:"spotsign button",
            imgShow: 'spotsign img',
            subBtn: 'imgView button[id="imgUpLoadSubBtn"]'
        },
        control: {
            camBtn: {
                tap: function (img) {
                    var that = this;
                    navigator.camera.getPicture(function (imageData) {
                        Ext.Viewport.mask({xtype: 'loadmask'});
                        //存储图片地址
                        that.setImgURL(imageData);
                        //展示图片
                        console.log(imageData);
                        Ext.Viewport.setActiveItem(Ext.create("qjtravel.view.ImgView"))
                        Ext.getCmp("imgShow").setSrc(imageData);
                        Ext.Viewport.unmask();
                    }, function (message) {
                        cordova.exec(function (message) {
                        }, function (error) {
                        }, "Messages", "showMsg", [message]);
                    }, {
                        quality: 45,
                        destinationType: Camera.DestinationType.FILE_URI,
                        sourceType: Camera.PictureSourceType.CAMERA,
                        mediaType: Camera.MediaType.PICTURE,
                        allowEdit: true
                    });

                }
            },
            subBtn: {
                //点击确定上传图片
                tap: "upLoadBtnSubFn"
            }
        }
    },
    /**
     * 点击确定提交表单,上传图片
     */
    upLoadBtnSubFn: function () {
        var imgCaption = Ext.getCmp("imgCaption").getValue(),
            imgShowValue = this.getImgShow().getSrc();
        if (Ext.isEmpty(imgCaption) || imgCaption.length < 2) {
            cordova.exec(function (message) {
            }, function (error) {
            }, "Messages", "showMsg", ['请完善照片描述']);
            return false;
        }
        if (Ext.isEmpty(imgShowValue)) {
            cordova.exec(function (message) {
            }, function (error) {
            }, "Messages", "showMsg", ['请完善照片信息']);
            return false;
        }
        var u_did=localStorage.getItem("udid");
        console.log("Controller中取到udid:"+u_did);
        console.log("image uploading......");
        Ext.Viewport.mask({xtype: 'loadmask'});

        var options = new FileUploadOptions();

        options.fileKey = "Filedata";//用于设置参数，对应form表单里控件的name属性，这是关键，废了一天时间，完全是因为这里，这里的参数名字，和表单提交的form对应

        options.fileName = this.getImgURL().substr(this.getImgURL().lastIndexOf('/') + 1);

        //如果是图片格式，就用image/jpeg，其他文件格式上官网查API

        options.mimeType = "image/jpeg";

        //请求地址
        var requestURL = encodeURI(Global.api_url + "/cloud/1/photowall_add");

        options.chunkedMode = false;

        var params = new Object();
        params.udid = u_did;
        params.caption = imgCaption;
        params.Filedata = this.getImgURL();

        options.params = params;
        var ft = new FileTransfer();
        ft.upload(this.getImgURL(), requestURL, function (succ) {
            console.log("image upload success!......");
            cordova.exec(function (msg) {
            }, function (msg) {
            }, "Messages", "showMsg", ["上传成功"]);
            Ext.Viewport.remove(Ext.Viewport.getActiveItem());
            //reload list store
            var s = Ext.create("qjtravel.store.ProductList");
            s.getProxy().setExtraParam("udid", u_did);
            s.load();
            Ext.getCmp("productlist").setStore(s);
            Ext.Viewport.unmask();

        }, function (error) {
            console.log("image upload error!......");
            console.log(error);
            cordova.exec(function (error) {
            }, function (error) {
            }, "Messages", "showMsg", ['服务器繁忙,稍后再试!']);
            Ext.Viewport.unmask();
        }, options);

    }
});